// pages/userInfoCenter/index.js

const App = getApp();
const { Dialog, Toast } = require("../../../config/modal")
const utils = require("../../../utils/util")
const { LOCATION } = require("../../../config/login.js")

Page({
    /**
     * 页面的初始数据
     */
    data: {
        title: "招聘信息发布",
        theamTextColor: App.globalData.theamTextColor,
        capsuleBarHeight: App.$getMenuButtonHeight(),
        item: {
            id: '',
            content: "",
            jobCode: "",
            title: "",
            price: '',
            schoolName: '',
        },
        productType: [
            {
                name: "面议"
            },
            {
                name: "5-15/小时"
            },
            {
                name: "15-30/小时"
            },
            {
                name: "30-60/小时"
            },
            {
                name: "80-120/天"
            }
            ,
            {
                name: "120-200/天"
            }
            ,
            {
                name: "200-500/天"
            }
            ,
            {
                name: "1000-2000/月"
            }
            ,
            {
                name: "3000-5000/月"
            }
            ,
            {
                name: "5000-8000/月"
            }

        ],
        typeName: '',
        showPopup: false,

    },
    URL: {
        detail: "/partTimeJob/detail/?",
        save: "/partTimeJob/save",
        update: "/partTimeJob/update",
    },
    page: {
        schoolPage: "/pages/school/index",
        partTimeJob: '/pages/recruit/list/index'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideShareMenu();
        this.initUserInfo();
        this.edit(options);
    },
    onShow() {
    },
    edit(options) {
        let jobCode = options.jobCode;
        if (utils.isInvalid(jobCode)) {
            return;
        }
        App.$get(true, this.URL.detail.replace("?", jobCode), {}, res => {
            this.setData({
                item: res.data
            })
        })
    },

    initUserInfo() {
        let user = App.$getUserInfo();
        this.setData({
            'item.schoolName': user.schoolName,
        })
    },

    // 获取当前所在位置
    toSelectSchoolName() {
        App.$navigateTo(this.page.schoolPage, false)
    },
    onSchoolNameChange(e) {
        this.setData({
            'item.schoolName': e.detail,
        })
    },

    onChangeFormField(e) {
        let key = 'item.' + utils.getEvent(e).name
        this.setData({
            [key]: e.detail.value == undefined ? e.detail : e.detail.value,
        })
    },


    // 选择地址
    selectType(e) {
        let item = utils.getEvent(e).item;
        this.setData({
            'item.price': item.name,
            typeName: item.name,
        })
        this.closePopup();
    },

    toSelectProductType() {
        this.setData({
            showPopup: true,
        })
    },

    closePopup() {
        this.setData({
            showPopup: false,
        })
    },

    onNormalChange(e) {
        let v = 'normal'
        if (e.detail) {
            v = 'urgent';
        }
        this.setData({
            'item.urgentState': v
        })
    },

    // 提交地址信息
    submitInfo() {


        if (utils.isInvalid(this.data.item.content)) {
            Toast('需要输入内容哦 😊～');
            return;
        }


        if (utils.isInvalid(this.data.item.title)) {
            Toast("请输入招聘标题")
            return;
        }

        if (utils.isInvalid(this.data.item.price)) {
            Toast("请填写酬劳")
            return;
        }


        if (utils.isInvalid(this.data.item.jobCode)) {
            this.save();
            return;
        }
        this.update();
    },

    save() {
        App.$post(true, this.URL.save, this.data.item, res => {
            this.reqResult(res);
        })
    }
    ,
    update() {

        let body = this.data.item;
        delete body['userInfo']
        delete body['updateTime']
        delete body['createTime']
        delete body['deleted']
        App.$post(true, this.URL.update, this.data.item, res => {
            this.reqResult(res);
        })
    }
    ,
    reqResult(res) {
        if (res.R) {
            Dialog.alert({
                message: res.msg,
                width: "250px",
                confirmButtonColor: "#D2691E",
                confirmButtonText: "确定",
            }).then(() => {
                App.$redirectTo(this.page.partTimeJob, true);
            })
        }
    }
    , goBack() {
        wx.navigateBack();
    }

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    , onReady: function () {

    }
    ,


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    }
    ,

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    }
    ,

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
    }
    ,

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
    ,

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
